<div id="show">
		
	 <div class="clear"></div>
		<div align="right">
			{if $is_coach eq '0'}
			<!--label>Become a Coach :</label> <input type="radio" name="user_type" value="1" onclick="coach_game()" /-->
			<a class="button" href="coach_game_update.php?profile=Y"><span>Become a Coach</span></a>
			{/if}
			{if $is_partner eq '0'}
			<a class="button" href="training_partner_game_update.php?profile=Y">Become Training Partner</a>
			<!--label>Become a Training Partner :</label> <input type="radio" name="user_type" value="3" onclick="tp_game()"/-->
			{/if} 
		</div>
		<div class="content"> 
   		
		{* Start loading Tabs *}
		{include file='user_tabs.tpl' active_tab='schedule'}
		{* End loading Tabs *}
		
        <div class="clear"></div>
        <div class="tabular-content" style="height: 460px;">
			<div class="total-message">&nbsp;&nbsp;</div> 
			<div class="clear"></div> 
			<div class="content-wrapper" style="position: relative;">
				
				<div class="schedule-links">
					<a href="javascript:;" onclick="changeSelection(this);loadPast();">Past</a>
					<span class="">|</span>
					<a href="javascript:;" class="selected" onclick="changeSelection(this);loadToday(0);">Today</a>
					<span class="">|</span>
					<a href="javascript:;" onclick="changeSelection(this);loadToday(1)">Tomorrow</a>
					<span class="">|</span>
					<a href="javascript:;" onclick="changeSelection(this);loadWeek();">This Week</a>
					<span class="">|</span>
					<a href="javascript:;" onclick="changeSelection(this);loadMonth();" >This Month</a>
					<span class="">|</span>
					<a href="javascript:;" onclick="changeSelection(this);loadAll()">All</a>
				</div>
				<div id="calendar-wrap">
				</div>
				<div id="calendar-info" style="left: 31%;">
				</div>
				<div id="info_loading" class="schedule_loading" style="margin-left: 700px;"></div>
				<div class="clear">
				</div>
				<div id="schedule_loading" class="schedule_loading" style="margin-left: 170px;"></div>
				<script language="JavaScript">
					function getUserId(){ldelim}return {$userId};{rdelim} 
				</script>
				{literal}
				
				<script type="text/javascript" src="jss/date-utils.js"></script>
				
				<script language="javascript">
					var ownerinfo={};
					var fullDayTime = ["0:00","0:30","1:00","1:30","2:00","2:30","3:00","3:30","4:00","4:30","5:00","5:30","6:00","6:30","7:00","7:30",
					"8:00","8:30","9:00","9:30","10:00","10:30","11:00","11:30","12:00","12:30","13:00","13:30","14:00","14:30","15:00",
					"15:30","16:00","16:30","17:00","17:30","18:00","18:30","19:00","19:30","20:00","20:30","21:00","21:30","22:00","22:30","23:00","23:30","0:00"];
					var calendar={};
					var selectedSlots={};
					var selectedHour={};
					var selectedOption=1;
					function loadToday(shift){
						
						if($('#schedule_loading').is(':visible'))
							$('#schedule_loading').fadeIn('fast');
						$("#calendar-wrap").empty();
						$("#calendar-info").empty();
						var date = new Date();
						date.setHours(0,0,0,0)
						var time = (date.getTime()/1000)+(3600)
						var day = date.getDate();
						var month = date.getMonth() +1;
						day = day+shift;
						selectedOption = 1+shift;
						
						var events = calendar.events;
						var event;
						var quantEvents = 0;
						for(var i = 0; i<events.length; i++){
							event = events[i];
							
							if(event[3] == day && event[4] == month){
								var time = event[2];
								var lasthour;
								for(var j = 0; j<event[6].length; j++){
									var hour = event[6][j];
									var html = [];
									if(hour.isEmpty || hour.availableSpot == 1){
										lasthour = undefined; 
										continue;
									}
									if(lasthour && lasthour.calendarRef == hour.calendarRef && lasthour.requestoruserId == hour.requestoruserId){
										continue;
									}
									
									html.push('<div class="schedule_event');
									if(hour.status == 3)
										html.push(' schedule_canceled');
									
									html.push('">');
									
									if(hour.requestoruserId == hour.userId)
										html.push('<div class="schedule_type">Traning session</div>');
									else
										html.push('<div class="schedule_type">Coach session</div>');
									
									if(j%2 == 0)
										date.setHours((j/2),0,0,0);
									else
										date.setHours((j/2),30,0,0);
									
									date.setDate(parseInt(event[3]));
									date.setMonth(parseInt(event[4])-1);	
																
									html.push('<div class="schedule_date">'+date.dateFormat('D, M j, Y h:i a')+'</div>');
									html.push('<div class="clear"></div>');
									
									html.push('</div>');
									lasthour = hour;
									var div = $(html.join(""));
									hour.index=j;
									$('#calendar-wrap').append(bindHover(div,hour));
									quantEvents++;
								}
								
								break;
							}
						}
						if(quantEvents == 0)
							$('#calendar-wrap').append("No Events");
							
						$('#schedule_loading').fadeOut('fast');
					}
					function loadMonth(){
						selectedOption = 4;
						$('#schedule_loading').fadeIn('fast');
						$("#calendar-wrap").empty();
						$("#calendar-info").empty();
						var date = new Date();
						date.setHours(0,0,0,0)
						var month = date.getMonth() +1;
						$("#calendar-wrap").empty();
						
						var events = calendar.events;
						var event;
						var quantEvents = 0;
						for(var i = 0; i<events.length; i++){
							event = events[i];
							if(event[4] == month && parseInt(event[3]) >= date.getDate()){
								var time = event[2];
								
								var lasthour;
								for(var j = 0; j<event[6].length; j++){
									var hour = event[6][j];
									var html = [];
									if(hour.isEmpty|| hour.availableSpot == 1){
										lasthour = undefined; 
										continue;
									}
									if(lasthour && lasthour.calendarRef == hour.calendarRef && lasthour.requestoruserId == hour.requestoruserId){
										continue;
									}
									html.push('<div class="schedule_event');
									if(hour.status == 3)
										html.push(' schedule_canceled');
									
									html.push('">');
									
									if(hour.requestoruserId == hour.userId)
										html.push('<div class="schedule_type">Traning session</div>');
									else
										html.push('<div class="schedule_type">Coach session</div>');
									
									if(j%2 == 0)
										date.setHours((j/2),0,0,0);
									else
										date.setHours((j/2),30,0,0);
										
									date.setDate(parseInt(event[3]));
																		
									html.push('<div class="schedule_date">'+date.dateFormat('D, M j, Y h:i a')+'</div>');
									html.push('<div class="clear"></div>');
									
									html.push('</div>');
									lasthour = hour;
									var div = $(html.join(""));
									hour.index=j;
									$('#calendar-wrap').append(bindHover(div,hour));
									quantEvents++;
								}
								
							}
						}
						if(quantEvents == 0)
							$('#calendar-wrap').append("No Events");
						$('#schedule_loading').fadeOut('fast');
					}
					
					
					function loadPast(){
						selectedOption = 0;
						$('#schedule_loading').fadeIn('fast');
						$("#calendar-wrap").empty();
						$("#calendar-info").empty();
						var date = new Date();
						var dateaux = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0, 0);
						var reference_time = (dateaux.getTime()/1000)+3600;
						$("#calendar-wrap").empty();
						
						var events = calendar.events;
						var event;
						var quantEvents = 0;
						for(var i = 0; i<events.length; i++){
							event = events[i];
							if(parseInt(event[2]) < reference_time){
								
								var lasthour;
								for(var j = 0; j<event[6].length; j++){
									var hour = event[6][j];
									var html = [];
									if(hour.isEmpty|| hour.availableSpot == 1){
										lasthour = undefined; 
										continue;
									}
									if(lasthour && lasthour.calendarRef == hour.calendarRef && lasthour.requestoruserId == hour.requestoruserId){
										continue;
									}
									
									html.push('<div class="schedule_event');
									if(hour.status == 3)
										html.push(' schedule_canceled');
									
									html.push('">');
									
									if(hour.requestoruserId == hour.userId)
										html.push('<div class="schedule_type">Traning session</div>');
									else
										html.push('<div class="schedule_type">Coach session</div>');
									
									if(j%2 == 0)
										date.setHours((j/2),0,0,0);
									else
										date.setHours((j/2),30,0,0);
									
									date.setDate(parseInt(event[3]));
									date.setMonth(parseInt(event[4])-1);						
									html.push('<div class="schedule_date">'+date.dateFormat('D, M j, Y h:i a')+'</div>');
									html.push('<div class="clear"></div>');
									
									html.push('</div>');
									lasthour = hour;
									
									var div = $(html.join(""));
									hour.index=j;
									$('#calendar-wrap').append(bindHover(div,hour));
									quantEvents++;
								}
								
							}
							$('#schedule_loading').fadeOut('fast');
						}
						if(quantEvents == 0)
							$('#calendar-wrap').append("No Events");
					}
					
					function loadWeek(){
						selectedOption = 2;
						$('#schedule_loading').fadeIn('fast');
						$("#calendar-wrap").empty();
						$("#calendar-info").empty();
						var date = new Date();
						date.setHours(0,0,0,0)
						var day = date.getDate();
						var month = date.getMonth() +1;
						var weekDay = date.getDay();
						var weekDays = "";
						var maxDay = day;
						for(var i = weekDay; i<7; i++){
							weekDays = weekDays+i;
							maxDay++;
						}
						var weekOfYear = date.getWeekOfYear();
					
						var events = calendar.events;
						var event;
						var quantEvents = 0;
						for(var i = 0; i<events.length; i++){
							event = events[i];
							var eventDate = new Date(parseInt(event[2])*1000);
							if(weekDays.indexOf(eventDate.getDay())!=-1 && parseInt(event[3]) < maxDay && event[4] == month && weekOfYear==eventDate.getWeekOfYear()){
								var time = event[2];
								
								var lasthour;
								for(var j = 0; j<event[6].length; j++){
									var hour = event[6][j];
									var html = [];
									if(hour.isEmpty|| hour.availableSpot == 1){
										lasthour = undefined; 
										continue;
									}
									
									if(lasthour && lasthour.calendarRef == hour.calendarRef && lasthour.requestoruserId == hour.requestoruserId){
										continue;
									}
									
									html.push('<div class="schedule_event');
									if(hour.status == 3)
										html.push(' schedule_canceled');
									
									html.push('">');
									
									if(hour.requestoruserId == hour.userId)
										html.push('<div class="schedule_type">Traning session</div>');
									else
										html.push('<div class="schedule_type">Coach session</div>');
									
									if(j%2 == 0)
										date.setHours((j/2),0,0,0);
									else
										date.setHours((j/2),30,0,0);
									
									date.setDate(parseInt(event[3]));
									date.setMonth(parseInt(event[4])-1);								
									html.push('<div class="schedule_date">'+date.dateFormat('D, M j, Y h:i a')+'</div>');
									html.push('<div class="clear"></div>');
									
									html.push('</div>');
									lasthour = hour;
									
									var div = $(html.join(""));
									hour.index=j;
									$('#calendar-wrap').append(bindHover(div,hour));
									quantEvents++;
									
								}
								
							}
						}
						if(quantEvents == 0)
							$('#calendar-wrap').append("No Events");
						$('#schedule_loading').fadeOut('fast');
					}

					function loadAll(){
						selectedOption = 5;
						$('#schedule_loading').fadeIn('fast');
						$("#calendar-wrap").empty();
						$("#calendar-info").empty();
						var date = new Date();
						var fixdate = new Date();
						fixdate.setHours(0,0,0,0);
						$("#calendar-wrap").empty();
						
						var events = calendar.events;
						var event;
						var quantEvents = 0;
						for(var i = 0; i<events.length; i++){
							event = events[i];
							
							if(parseInt(event[2])*1000 >= fixdate.getTime()){
								var time = event[2];
								
								var lasthour;
								for(var j = 0; j<event[6].length; j++){
									var hour = event[6][j];
									var html = [];
									if(hour.isEmpty|| hour.availableSpot == 1){
										lasthour = undefined; 
										continue;
									}
									if(lasthour && lasthour.calendarRef == hour.calendarRef && lasthour.requestoruserId == hour.requestoruserId){
										continue;
									}
									
									html.push('<div class="schedule_event');
									if(hour.status == 3)
										html.push(' schedule_canceled');
									
									html.push('">');
									
									if(hour.requestoruserId == hour.userId)
										html.push('<div class="schedule_type">Traning session</div>');
									else
										html.push('<div class="schedule_type">Coach session</div>');
									
									if(j%2 == 0)
										date.setHours((j/2),0,0,0);
									else
										date.setHours((j/2),30,0,0);
									
									date.setDate(parseInt(event[3]));
									date.setMonth(parseInt(event[4])-1);									
									html.push('<div class="schedule_date">'+date.dateFormat('D, M j, Y h:i a')+'</div>');
									html.push('<div class="clear"></div>');
									
									html.push('</div>');
									lasthour = hour;
									
									var div = $(html.join(""));
									hour.index=j;
									$('#calendar-wrap').append(bindHover(div,hour));
									quantEvents++;
								}
							}
						}
						if(quantEvents == 0)
							$('#calendar-wrap').append("No Events");
						$('#schedule_loading').fadeOut('fast');
					}
					function changeSelection(element){
						$(".schedule-links a").removeClass("selected");
						$(element).addClass("selected");
					}
					
					function bindHover(div,hour) {
						return div.bind('mouseout', function(event) {
								$(this).removeClass("schedule_event_over");
							})
							.bind('mouseover', function(event) {
								$(this).addClass("schedule_event_over");
							})
							.bind('click', function(event) {
								$(".schedule_event").removeClass("schedule_event_selected");
								$(this).addClass("schedule_event_selected");
								getScheduleInfo(hour);
							});
					}
					function getScheduleInfo(hour){ 
						selectedHour = hour;
						
						var html = [];
						html.push('<div class="schedule-info-wrap"><div class="scheduleTitle">Schedule information</div>');		
						$("#calendar-info").empty();
						$('#info_loading').fadeIn('fast');
						if(hour.userId == hour.requestoruserId){//is trainee
							$('#calendarloading').fadeIn('fast');
							$('#calendaroverlay').fadeIn('fast');
							var urlcalendarId = "datafeed.php?method=getcalendarbyid&calendarId="+hour.calendarRef;
							$.get(urlcalendarId, function(data){
								var urlUser = "datafeed.php?method=getUserInfo&userid="+data.user_id;
								$.get(urlUser, function(data){
									var coachname = data.name;
									var gamename = "";
									var rate = "";
									for (var i = 0; i<hour.gameId.length; i++){
										for (var j = 0; j < data.games.length; j++){
											if(hour.gameId[i] == data.games[j].game_id){
												gamename = data.games[j].game_name;
												rate = data.games[j].rate;
											}
										}
									}
									var url = "datafeed.php?method=getcalendarrefsequence&index="+hour.index+"&calendarId="+hour.calendarId;
									$.get(url, function(data){
										//var x;
										var seq = $.parseJSON(data);
										var values = seq.values;
										var slots = values.split('#');
										slots.sort();
										selectedSlots = slots;
										//if(hour.status==3)
										//	removeCanceled();
										var table = [];
										table.push('<div class"wrap">');
										for(var i = 0; i<(slots.length); i++){
											if(i == 0 || i == 16 || i == 32)
												table.push('<div style="float: left; margin-right: 15px;">');
											
											var a = fullDayTime[parseInt(slots[i])];
											var b = fullDayTime[parseInt(slots[i+1])];
											if(!b){
												var aux = parseInt(slots[i]);
												b =fullDayTime[aux+1];
											}
											table.push('<span>'+a+' - '+b+'</span><br/>');
											if(i == 15 || i == 31)
												table.push('</div>');
										}
										html.push('<table cellspacing="7px" cellpadding="0">');		
										html.push(getTwoCellRow('Coach:', coachname));
										html.push(getTwoCellRow('Game:', gamename));
										html.push(getTwoCellRow('Rate:', rate));
										html.push(getTwoCellRow('Sequence:', table.join("")));
										html.push('</table>');
										if(hour.status==3){
											html.push('<div style="font-weight:bold; color: #c22c04; font-size: 16px">This schedule have been canceled</div>');
											removeCanceled();
										}else
											html.push('<a href="javascript:;" onclick="cancel()" class="button">Cancel</a>');
										html.push('</div>');
										$('#info_loading').fadeOut('fast');
										$("#calendar-info").html(html.join(""));
									}, "json");
								}, "json");
							
							}, "json");
							
						}else if(hour.userId != hour.requestoruserId){//is Coach
							var urlUser = "datafeed.php?method=getUserInfo&userid="+hour.requestoruserId;
							$.get(urlUser, function(data){
								var gamename = "";
								var rate = "";
								for (var i = 0; i<hour.gameId.length; i++){
									for (var j = 0; j < ownerinfo.games.length; j++){
										if(hour.gameId[i] ==  ownerinfo.games[j].game_id){
											gamename =  ownerinfo.games[j].game_name;
											rate = ownerinfo.games[j].rate;
										} 
									}
								}
								var traineename = data.name;
								var url = "datafeed.php?method=getSequence&index="+hour.index+"&calendarId="+hour.calendarId;
								$.get(url, function(data){
									//var x;
									var seq = $.parseJSON(data);
									var values = seq.values;
									var slots = values.split('#');
									slots.sort();
									selectedSlots = slots;
									//if(hour.status==3)
									//	removeCanceled();
									var table = [];
									table.push('<div class"wrap">');
									for(var i = 0; i<(slots.length); i++){
										if(i == 0 || i == 16 || i == 32)
											table.push('<div style="float: left; margin-right: 15px;">');
										
										var a = fullDayTime[parseInt(slots[i])];
										var b = fullDayTime[parseInt(slots[i+1])];
										if(!b){
											var aux = parseInt(slots[i]);
											b =fullDayTime[aux+1];
										}
										table.push('<span>'+a+' - '+b+'</span><br/>');
										if(i == 15 || i == 31)
											table.push('</div>');
									}
									html.push('<table cellspacing="7px" cellpadding="0">');		
									html.push(getTwoCellRow('Trainee:', traineename));
									html.push(getTwoCellRow('Game:', gamename));
									html.push(getTwoCellRow('Sequence:', table.join("")));
									html.push('</table>');
									//html.push('<div class="button-flex ">');
									if(hour.status==3){
										html.push('<div style="font-weight:bold; color: #c22c04; font-size: 16px">This schedule have been canceled</div>');
										removeCanceled();
									}else
										html.push('<a href="javascript:;" onclick="cancel()" class="button">Cancel</a>');
									//html.push('</div>');
									html.push('</div>');
									$('#info_loading').fadeOut('fast');
									$("#calendar-info").html(html.join(""));
								}, "json");
							}, "json");
						}
					}
					function getTwoCellRow(arg0, arg1){
						var html = [];
						html.push('<tr valign="top"><td><div class="scheduleLabel">');
						html.push(arg0);
						html.push('</div></td><td>');
						html.push(arg1);
						html.push('</td></tr>');
						return html.join("");
					}
					
					function cancel(cancelClientEvents){
						//selectedSlots;
						//selectedHour;
						var slots = [];
						for(var i = 0; i< selectedSlots.length; i++){
							slots.push(parseInt(selectedSlots[i]));
						}
						if(!cancelClientEvents)
							var cancelClientEvents = 0;
						$.post("datafeed.php",{"method": "cancel", "userid": selectedHour.userId, "schedules": slots.join(),"date": selectedHour.date,"cancelClientEvents":cancelClientEvents,"originalDate":1}, function(data){
							if(data.IsSuccess){
								
							}else if(data.ErrorCode == -4 ){
								var html = [];
								html.push('<div class="schedule_confirmation" id="schedule_confirmation" style="position: absolute;">');
								html.push('<table style="width: 100%;"><tr align="center" valign="middle"><td style="height: 95px;"  ><span class="time_calendar_warnig_text">');
								html.push('You cancelling events events that have alredy been booked by a trainee, are you sure?');
								html.push('</span></td></tr><tr align="center" valign="middle"><td align="center" style="height: 35px;">');
								html.push('<a style="" href="#" class="button" onclick="$(\'#schedule_confirmation\').remove();cancel(\'1\');$(\'#overlay\').fadeOut(\'fast\',loadCalendar())"><span>Yes</span></a>');
								html.push('<a style="" href="#" class="button" onclick="$(\'#schedule_confirmation\').remove();$(\'#overlay\').fadeOut(\'fast\')"><span>No</span></a>');
								html.push('</td></tr></div>');
								
								var warnig = $(html.join("")).draggable();
								$('body').append(warnig);
								$("#overlay").fadeIn('fast');
							
							
								//$.timecalendar._showYesNoValidation("You cancelling events events that have alredy been booked by a trainee, are you sure?",'DP_jQuery_' + dpuuid + '.timecalendar.cancel(\'1\')');
								
							}
						},"json");
					}
					
					function removeCanceled(){
		
						var slots = [];
						for(var i = 0; i< selectedSlots.length; i++){
							slots.push(parseInt(selectedSlots[i]));
						}
						//$.post("datafeed.php",{"method": "cancel", "userid": selectedHour.userId, "schedules": slots.join(),"date": selectedHour.date,"cancelClientEvents":cancelClientEvents,"originalDate":1}, function(data){
						$.post("datafeed.php",{"method": "removeSlots", "userid": selectedHour.userId, "schedules": slots.join(),"date": selectedHour.date,"originalDate":1}, function(data){
							//$.timecalendar._customSelectDayAction($.timecalendar.selectedDay);
							
							//TODO: add/remove points
						}, 
						"json");
					}
					function loadCalendar(){
						calendar={};
						selectedSlots={};
					    selectedHour={};
						
						$('#schedule_loading').fadeIn('fast');
						var url = "datafeed.php?method=getAllEvents";
						$.get(url, function(data){
							calendar = data;
							calendar.events.sort(function (a, b){
								return parseInt(a[2])-parseInt(b[2]);
							});
							
							var urlUser = "datafeed.php?method=getUserInfo&userid="+getUserId();
							$.get(urlUser, function(data){
								ownerinfo = data;
								//selectedOption = 5;
								//loadToday(0);
								switch (selectedOption) {
									case 0: loadPast();
											break;
									case 1: loadToday(0);
											break;
									case 2: loadToday(1);
											break;
									case 3: loadWeek();
											break;
									case 4: loadMonth();
											break;
									case 5: loadAll();
											break;
									default: loadToday(0);;
								}
								
								
							},"json");
						},"json");
					}
					
					loadCalendar();
					
				</script>
				{/literal}
			</div>
		</div>
	</div>
</div>